package com.learn;

import com.tadu.InputField;
import com.tadu.InputUtil;

import java.util.List;

public class Demo {
    public static void main(String[] args) {
        //用户注册
        //regUser();
        //用户登录
       // loginUser();
        //修改用户信息
        //updateUser();
        //查询所有用户信息
        selectAllUser();
    }

    /*
    * 查询所有用户信息
    * */
    public static void selectAllUser(){
        UserMapper userMapper = new UserMapper();
        List<User> users = userMapper.selectAll();
        if(users != null){
            for (User user : users) {
                System.out.println(user);
            }
        }else{
            System.out.println("查询失败");
        }
    }

    /*
    * 修改用户信息
    * */
    public static void updateUser() {
        User user = InputUtil.getInputObject(new User(), "修改用户信息");
        System.out.println(user);//看看是否能查询到输入的信息

        UserMapper userMapper = new UserMapper();
        int num = userMapper.update(user);
        System.out.println(num > 0 ? "修改成功" : "修改失败");
    }

    /*
     * 如果用户的用户名输入错误，提示用户名错误，如果密码错误，提示密码错误
     * */
    public static void loginUser() {

        UserLoginDTO dto = InputUtil.getInputObject(new UserLoginDTO(), "欢迎登录");
        System.out.println(dto);

        UserMapper userMapper = new UserMapper();
        User user = userMapper.selectByName(dto.getUsername());
        System.out.println(user);//看看是否能查询到
        if (user == null) {
            System.out.println("用户名不存在");
            return;
        } else {
            if (user.getPassword().equals(dto.getPassword())) {
                System.out.println("登录成功，欢迎回来：" + user.getName());
            } else {
                System.out.println("密码错误");
            }
        }
    }


    /*
     * 用户注册操作
     * */
    public static void regUser() {
        User user = InputUtil.getInputObject(new User(), "欢迎注册");
        System.out.println(user);

        UserMapper userMapper = new UserMapper();

        //判断用户名是否存在
        if(userMapper.selectByName(user.getName()) == null){
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "恭喜您注册成功" : "注册失败了");
        }else{
            System.out.println("该用户已存在");
        }
    }
}
